package com.xoa.service.workflow.flowtype.impl;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

import com.xoa.dao.notify.NotifyMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.dao.workflow.*;
import com.xoa.model.notify.Notify;
import com.xoa.model.sms.SmsBody;
import com.xoa.model.sms2.Sms2Priv;
import com.xoa.model.users.Users;
import com.xoa.model.workflow.*;
import com.xoa.service.WFE.WFELog.WFELog;
import com.xoa.service.notify.NotifyService;
import com.xoa.service.sms.SmsService;
import com.xoa.service.smsDelivery.impl.Sms2PrivServiceImpl;
import com.xoa.service.users.UsersService;
import com.xoa.service.work.WorkService;
import com.xoa.service.workflow.flowModelAssociationSet.FlowAssociationSetService;
import com.xoa.util.*;
import com.xoa.util.common.L;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xoa.model.workflow.FlowRunPrcs;
import com.xoa.service.workflow.flowtype.FlowRunPrcsService;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class FlowRunPrcsServiceImpl implements FlowRunPrcsService {

	@Autowired
	private WFELog wfeLog;

	@Resource
	private FlowRunPrcsMapper flowRunPrcsMapper;

	@Resource
	private UsersService usersService;

	@Resource
	private FlowTypeModelMapper flowTypeModelMapper;

	@Resource
	private FlowProcessMapper flowProcessMapper;

	@Resource
	private FlowRunMapper flowRunMapper;

	@Resource
	private UsersMapper usersMapper;

	@Resource
	private SmsService smsService;

	@Resource
	private Sms2PrivServiceImpl sms2PrivServiceImpl;

	@Resource
    private FlowAssociationSetService flowAssociationSetService;

	@Autowired
	private WorkService workService;


	@Override
	public void save(FlowRunPrcs flowRunPrcs) {
		flowRunPrcsMapper.insertSelective(flowRunPrcs);
	}

	/**
	 * 创建作者:   张勇
	 * 创建日期:   2017/5/24 20:29
	 * 方法介绍:   查询待办工作
	 * 参数说明:  修整时间计算
	 *
	 * @return
	 */
	@SuppressWarnings("all")
	@Override
	public ToJson<FlowRunPrcs> selectObject(Map<String, Object> maps, Integer page,
											Integer pageSize, boolean useFlag) {
		ToJson<FlowRunPrcs> toJson = new ToJson<FlowRunPrcs>();
		PageParams pageParams = new PageParams();
		pageParams.setPage(page);
		pageParams.setPageSize(pageSize);
		pageParams.setUseFlag(useFlag);
		maps.put("page", pageParams);
		List<FlowRunPrcs> list = flowRunPrcsMapper.selectObjcet(maps);
		List<FlowRunPrcs> returnList = new ArrayList<FlowRunPrcs>();
		int len = list.size();
		if (len > 0) {
			toJson.setTotleNum(pageParams.getTotal());
			Date newDate = new Date();
			for (FlowRunPrcs flowRunPrcs : list) {
				flowRunPrcs.getFlowRun().setUserName(usersService.getUserNameById(flowRunPrcs.getFlowRun().getBeginUser()));
				Users users=usersMapper.findUsersByuserId(flowRunPrcs.getUserId());
				flowRunPrcs.setUid(users.getUid());
				flowRunPrcs.setUserName(usersService.getUserNameById(flowRunPrcs.getUserId()));
				flowRunPrcs.setFlowType(flowTypeModelMapper.queryOneObject(flowRunPrcs.getFlowRun().getFlowId()));
				//查询流程并设置流程
				FlowProcess flowProcess = flowProcessMapper.findProcess(flowRunPrcs.getFlowRun().getFlowId(), flowRunPrcs.getFlowPrcs());
				if (flowProcess != null) {
					flowRunPrcs.setFlowProcess(flowProcess);
				}

				if (DateFormat.getTime(flowRunPrcs.getPrcsTime()) != -621701856) {
//				if(!StringUtils.checkNull(flowRunPrcs.getPrcsTime())) {
					// 工作接收时间
					flowRunPrcs.setReceive(DateFormat.getStrTime(DateFormat.getTime(flowRunPrcs.getPrcsTime())));
					// 办理停留时间
					flowRunPrcs.setHandleTime(DateFormat.returnTime((DateFormat.getTime(DateFormat.getStrDate(newDate))) - DateFormat.getTime(flowRunPrcs.getPrcsTime())));
				}
				//工作创建时间
				String createTime = flowRunPrcs.getCreateTime();
				//计算时间转换
				if (DateFormat.getTime(flowRunPrcs.getPrcsTime()) != -621701856 && flowRunPrcs.getPrcsId() > 1 ||
						DateFormat.getTime(flowRunPrcs.getPrcsTime()) == -621701856 && flowRunPrcs.getPrcsId() > 1) {
					Integer runId = flowRunPrcs.getRunId();
					Integer prcsId = flowRunPrcs.getPrcsId() - 1;
					String userId = (String) maps.get("userId");
					maps.put("prcsId", prcsId);
					maps.put("runId", runId);
					maps.put("flowPrcs", flowRunPrcs.getFlowPrcs());
					// 到达时间
					flowRunPrcs.setReceiptTime(DateFormat.getStrTime(DateFormat.getTime(createTime)));
					// 到达停留时间
					flowRunPrcs.setArriveTime(DateFormat.returnTime((DateFormat.getTime(DateFormat.getStrDate(newDate))) - DateFormat.getTime(flowRunPrcs.getReceiptTime())));
				} else {
					flowRunPrcs.setReceiptTime(DateFormat.getStrTime(DateFormat.getTime(createTime)));
					flowRunPrcs.setArriveTime(DateFormat.returnTime((DateFormat.getTime(DateFormat.getStrDate(newDate))) - DateFormat.getTime(createTime)));
					flowRunPrcs.setReceive("");
					flowRunPrcs.setHandleTime("");
				}
				FlowRun flowRun = flowRunMapper.find(flowRunPrcs.getRunId());
				String userName = usersService.getUserNameById(flowRun.getBeginUser());
				flowRunPrcs.setUserName(userName);
				returnList.add(flowRunPrcs);
			}
			toJson.setFlag(0);
			toJson.setMsg("ok");
			toJson.setObj(returnList);
		} else {
			toJson.setFlag(1);
			toJson.setMsg("error");
		}
		return toJson;
	}

	/**
	 * 创建作者:   张勇
	 * 创建日期:   2017/5/24 20:30
	 * 方法介绍:   查询办结工作
	 * 参数说明:
	 *
	 * @return
	 */
	@Override
	public ToJson<FlowRunPrcs> selectEnd(Map<String, Object> maps, Integer page,
										 Integer pageSize, boolean useFlag) {
		ToJson<FlowRunPrcs> toJson = new ToJson<FlowRunPrcs>(1, "error");
		PageParams pageParams = new PageParams();
		pageParams.setPage(page);
		pageParams.setPageSize(pageSize);
		pageParams.setUseFlag(useFlag);
		maps.put("page", pageParams);
		List<FlowRunPrcs> list = flowRunPrcsMapper.selectEnd(maps);
		List<FlowRunPrcs> returnList = new ArrayList<FlowRunPrcs>();
		int len = list.size();
		if (len > 0) {
			toJson.setTotleNum(pageParams.getTotal());
			for (FlowRunPrcs flowRunPrcs : list) {
				FlowRun flowRun = flowRunMapper.find(flowRunPrcs.getRunId());
				//List<FlowRunPrcs> flowRunPrcsList = flowRunPrcsMapper.findByRunId(flowRunPrcs.getRunId());
				//查询下一步的流程
				Map<String, Object> map1 = new HashMap<String, Object>();
				map1.put("prcsId", flowRunPrcs.getPrcsId()+1);
				map1.put("runId", flowRunPrcs.getRunId());
				List<FlowRunPrcs>  nextFlowRunPrcsList = flowRunPrcsMapper.findNextFrp(map1);
				//查询流程最后一步
				List<FlowRunPrcs> maxFrp = flowRunPrcsMapper.findMaxFrp(flowRunPrcs.getRunId());

				if(nextFlowRunPrcsList.size()==1){
					FlowRunPrcs flowRunPrcs1 = nextFlowRunPrcsList.get(0);
					if (flowRunPrcs1.getPrcsFlag().equals("1")){
						flowRunPrcs.setState1("已转交，下一步未接收");
					}else if(flowRunPrcs1.getPrcsFlag().equals("2")){
						flowRunPrcs.setState1("已转交，下一步办理中");
					}else if(flowRunPrcs1.getPrcsFlag().equals("3")){
						flowRunPrcs.setState1("已转交，下一步已办结");
					}else if(flowRunPrcs1.getPrcsFlag().equals("4")){
						flowRunPrcs.setState1("已办结");
					}
				}else {
					List<String> list1 = new ArrayList<String>();
					int one = 0;
					int two = 0;
					int three = 0;
					int four = 0;
					for (FlowRunPrcs flowRunPrcs1 : nextFlowRunPrcsList){
						if (flowRunPrcs1.getPrcsFlag().equals("1")){
							one++;
						}else if(flowRunPrcs1.getPrcsFlag().equals("2")){
							two++;
						}else if(flowRunPrcs1.getPrcsFlag().equals("3")){
							three++;
						}else if(flowRunPrcs1.getPrcsFlag().equals("4")){
							four++;
						}
					}
					if (one==nextFlowRunPrcsList.size()){
						flowRunPrcs.setState1("已转交，下一步未接收");
					}else if (three==nextFlowRunPrcsList.size()){
						flowRunPrcs.setState1("已转交，下一步已办结");
					}else {
						flowRunPrcs.setState1("已转交，下一步办理中");
					}
				}
				if (maxFrp.size()>1){
					flowRunPrcs.setState2("1");
				}else {
					FlowRunPrcs flowRunPrcs3 = maxFrp.get(0);
					flowRunPrcs.setState2("0");

					Map<String, Object> map = new HashMap<String,Object>();
					map.put("runId", flowRunPrcs3.getRunId());
					map.put("prcsId", flowRunPrcs3.getPrcsId());
					String prcsName = "第"+flowRunPrcs3.getPrcsId()+"步:" +flowProcessMapper.findPrcsName(map);
					flowRunPrcs.setBz(prcsName);
					flowRunPrcs.setCurrentPeople(usersService.getUserNameById(flowRunPrcs3.getUserId()));
				}

				if (StringUtils.checkNull(flowRun.getEndTime())){
					flowRunPrcs.setState("执行中");
				}else {
					flowRunPrcs.setState("已结束");
					flowRunPrcs.setState1("已办结");
				}
				flowRunPrcs.getFlowRun().setUserName(usersService.getUserNameById(flowRunPrcs.getFlowRun().getBeginUser()));
				flowRunPrcs.setUserName(usersService.getUserNameById(flowRunPrcs.getUserId()));
				flowRunPrcs.setFlowType(flowTypeModelMapper.queryOneObject(flowRunPrcs.getFlowRun().getFlowId()));
				//查询流程并设置流程
				FlowProcess flowProcess = flowProcessMapper.findProcess(flowRunPrcs.getFlowRun().getFlowId(), flowRunPrcs.getFlowPrcs());
				if (flowProcess != null) {
					flowRunPrcs.setFlowProcess(flowProcess);
				}
				if (!StringUtils.checkNull(flowRunPrcs.getDeliverTime())) {
					flowRunPrcs.setDeliverTime(DateFormat.getStrTime(DateFormat.getTime(flowRunPrcs.getDeliverTime())));
				}
				returnList.add(flowRunPrcs);

				//到达时长
				flowRunPrcs.setArriveTime(DateFormat.returnTime((DateFormat.getTime(DateFormat.getStrDate(new Date()))) - DateFormat.getTime(flowRunPrcs.getCreateTime())));
				// 办理时长
				flowRunPrcs.setHandleTime(DateFormat.returnTime((DateFormat.getTime(DateFormat.getStrDate(new Date()))) - DateFormat.getTime(flowRunPrcs.getPrcsTime())));
				flowRunPrcs.setCreateTime(flowRunPrcs.getCreateTime().substring(0,flowRunPrcs.getCreateTime().length()-2));
				flowRunPrcs.setPrcsTime(flowRunPrcs.getPrcsTime().substring(0,flowRunPrcs.getPrcsTime().length()-2));
				flowRunPrcs.setJbbz("第"+flowRunPrcs.getPrcsId()+"步:"+flowRunPrcs.getFlowProcess().getPrcsName());
			}
			toJson.setFlag(0);
			toJson.setMsg("ok");
			toJson.setObj(returnList);
		} else {
			toJson.setFlag(1);
			toJson.setMsg("error");
		}
		return toJson;
	}

	/**
	 * 创建作者:   张勇
	 * 创建日期:   2017/5/25 15:47
	 * 方法介绍:   查询挂起工作
	 * 参数说明:
	 *
	 * @return
	 */
	@SuppressWarnings("all")
	@Override
	public ToJson<FlowRunPrcs> selectHang(Map<String, Object> maps, Integer page,
										  Integer pageSize, boolean useFlag) {
		ToJson<FlowRunPrcs> toJson = new ToJson<FlowRunPrcs>();
		PageParams pageParams = new PageParams();
		pageParams.setPage(page);
		pageParams.setPageSize(pageSize);
		pageParams.setUseFlag(useFlag);
		maps.put("page", pageParams);
		List<FlowRunPrcs> list = flowRunPrcsMapper.selectHang(maps);
		List<FlowRunPrcs> returnList = new ArrayList<FlowRunPrcs>();
		int len = list.size();
		if (len > 0) {
			toJson.setTotleNum(pageParams.getTotal());
			for (FlowRunPrcs flowRunPrcs : list) {
				flowRunPrcs.setUserName(usersService.getUserNameById(flowRunPrcs.getUserId()));
				flowRunPrcs.setFlowType(flowTypeModelMapper.queryOneObject(flowRunPrcs.getFlowRun().getFlowId()));
				returnList.add(flowRunPrcs);
			}
			toJson.setFlag(0);
			toJson.setMsg("ok");
			toJson.setObj(returnList);
		} else {
			toJson.setFlag(1);
			toJson.setMsg("error");
		}
		return toJson;
	}

	/**
	 * 创建作者:   张勇
	 * 创建日期:   2017/5/25 15:47
	 * 方法介绍:   查询所有工作
	 * 参数说明:
	 *
	 * @return
	 */
	@SuppressWarnings("all")
	@Override
	public ToJson<FlowRunPrcs> selectAll(Map<String, Object> maps, Integer page, Integer pageSize, boolean useFlag) {
		ToJson<FlowRunPrcs> toJson = new ToJson<FlowRunPrcs>(1, "error");
		PageParams pages = new PageParams();
		pages.setPage(page);
		pages.setPageSize(pageSize);
		pages.setUseFlag(useFlag);
		maps.put("page", pages);
		List<FlowRunPrcs> list = flowRunPrcsMapper.selectAll(maps);
		List<FlowRunPrcs> returnList = new ArrayList<FlowRunPrcs>();
		int len = list.size();
		if (len > 0) {
			toJson.setTotleNum(pages.getTotal());
			for (FlowRunPrcs flowRunPrcs : list) {

				FlowRun flowRun = flowRunMapper.find(flowRunPrcs.getRunId());
				if (StringUtils.checkNull(flowRun.getEndTime())){
					flowRunPrcs.setState("执行中");
				}else {
					flowRunPrcs.setState("已结束");
				}
				flowRunPrcs.getFlowRun().setUserName(usersService.getUserNameById(flowRunPrcs.getFlowRun().getBeginUser()));
				flowRunPrcs.setUserName(usersService.getUserNameById(flowRunPrcs.getUserId()));
				flowRunPrcs.setFlowType(flowTypeModelMapper.queryOneObject(flowRunPrcs.getFlowRun().getFlowId()));
				FlowProcess flowProcess = flowProcessMapper.findProcess(flowRunPrcs.getFlowRun().getFlowId(), flowRunPrcs.getFlowPrcs());
				if (flowProcess != null) {
					flowRunPrcs.setFlowProcess(flowProcess);
				}
				returnList.add(flowRunPrcs);
				flowRunPrcs.setJbbz("第"+flowRunPrcs.getPrcsId()+"步:"+flowRunPrcs.getFlowProcess().getPrcsName());
			}

			toJson.setFlag(0);
			toJson.setMsg("ok");
			toJson.setObj(returnList);
		} else {
			toJson.setFlag(1);
			toJson.setMsg("error");
		}
		return toJson;
	}

	/**
	 * 创建作者:   张勇
	 * 创建日期:   2017/6/20 13:52
	 * 方法介绍:   删除我的工作待办第一步
	 * 参数说明:
	 *
	 * @return
	 */
	@Override
	@Transactional
	public ToJson<FlowRunPrcs> deleteRunPrcs(FlowRunPrcs flowRunPrcs) {
		ToJson<FlowRunPrcs> toJson = new ToJson<FlowRunPrcs>(1, "error");
		try {
			Integer flag = flowRunPrcsMapper.selectPrcs(flowRunPrcs.getId());
			if (flag == 1) {
				flowRunPrcsMapper.deleteWork(flowRunPrcs);
				toJson.setFlag(0);
				toJson.setMsg("ok");
			} else {
				//重名
				toJson.setMsg("repetition");
			}
		} catch (Exception e) {
			L.e("FlowRunPrcsService deleteRunPrcs:" + e);
		}
		return toJson;
	}

	public List<FlowRunPrcs> findByRunId(Map<String, Object> maps) {
		List<FlowRunPrcs> l = flowRunPrcsMapper.selectfrp(maps);
		return l;

	}

	@Override
	@Transactional
	public void update(FlowRunPrcsExcted flowRunPrcs) {
		flowRunPrcsMapper.updateByPrimaryKeySelective(flowRunPrcs);
	}

	//	public  String returnNode (Integer runId){
//		String returnNodes = "";
//		try {
//			List<FlowRunPrcs> list = flowRunPrcsMapper.findAllNode(runId);
//			int leng = list.size();
//			for (int i = 0; i < leng; i++) {
//				String flag = list.get(i).getPrcsFlag();
//				if (!"3".equals(flag) || !"4".equals(flag)) {
//					list1.add(list.get(i));
//					break;
//				}
//			}
//		}catch (Exception e){
//			L.e("ReturnNode Error:"+e);
//		}
//		return toJson;
//	}
	@Transactional
	@Override
	public int updateSql(Map<String, Object> map) {
		int a = flowRunPrcsMapper.updateSome(map);
		return a;
	}

	@Override
	public List<FlowRunPrcs> findlessRunId(Map<String, Object> maps) {
		List<FlowRunPrcs> list = flowRunPrcsMapper.findlessRunId(maps);
		return list;
	}

	@Override
	public AjaxJson entrustWork(Map<String, Object> maps, Integer page,
								Integer pageSize, boolean useFlag) {
		AjaxJson ajaxJson = new AjaxJson();
		PageParams pages = new PageParams();
		pages.setPage(page);
		pages.setPageSize(pageSize);
		pages.setUseFlag(useFlag);
		maps.put("page", pages);
		List<FlowRunPrcs> list = flowRunPrcsMapper.entrustWork(maps);
		int len = list.size();
		if (len > 0) {
			ajaxJson.setTotleNum(pages.getTotal());
			for (FlowRunPrcs flowRunPrcs : list) {
				flowRunPrcs.setUserName(usersService.getUserNameById(flowRunPrcs.getUserId()));
				flowRunPrcs.setFlowType(flowTypeModelMapper.queryOneObject(flowRunPrcs.getFlowRun().getFlowId()));
				FlowProcess flowProcess = flowProcessMapper.findProcess(flowRunPrcs.getFlowRun().getFlowId(), flowRunPrcs.getFlowPrcs());
				if (flowProcess != null) {
					flowRunPrcs.setFlowProcess(flowProcess);
				}
			}
		}

		return null;

	}



	/**
	 * 创建作者:   zlf
	 * 创建日期:   2017/11/9 17:04
	 * 方法介绍:   查询是否强制转交
	 * 参数说明:
	 * @return
	 */
	public Integer checkGatherNode(Map<String, Object> maps){
		return flowRunPrcsMapper.checkGatherNode(maps);
	}


	@Override
	public AjaxJson monitoring( Map<String,Object> maps,Integer page,
								Integer pageSize, boolean useFlag){
		AjaxJson ajaxJson = new AjaxJson();
		PageParams pages = new PageParams();
		pages.setPage(page);
		pages.setPageSize(pageSize);
		pages.setUseFlag(useFlag);
		maps.put("page", pages);
		try {
			List<FlowRunPrcs> list = flowRunPrcsMapper.monitoring(maps);
			for (FlowRunPrcs flowRunPrcs : list){
				String time = this.reTime(flowRunPrcs.getPrcsTime());
				flowRunPrcs.setHandleTime(time);
				String prcsName = "第"+flowRunPrcs.getPrcsId()+"步"+":"+flowRunPrcs.getPrcsName();
				flowRunPrcs.setPrcsName(prcsName);
			}
			ajaxJson.setTotleNum(pages.getTotal());
			ajaxJson.setMsg("OK");
			ajaxJson.setFlag(true);
			ajaxJson.setObj(list);
		}catch (Exception e){
			ajaxJson.setMsg("false");
			ajaxJson.setFlag(false);
		}
		return ajaxJson;
	}


	public static String reTime(String prcsTime)throws Exception{
		String format = "yyyy-MM-dd HH:mm:ss";
		long aString = System.currentTimeMillis()/1000;
		Integer time = Integer.parseInt(String.valueOf(aString));

		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Long aa = sdf.parse(prcsTime).getTime()/1000;

		Integer end = Integer.parseInt(String.valueOf(aa));
		Integer re = time-end;
		String aString3 = DateFormat.returnTime(re);
		return aString3;
	}

	/**
	 * 创建作者:   牛江丽
	 * 创建日期:   2017/6/20 13:52
	 * 方法介绍:   流程委托
	 * 参数说明:
	 * @return
	 */
	//run_id:1776,
	// prcs_key_id:3175,
	// prcs_id:2,
	// flow_prcs:3,
	// to_user:zhaomin, 委托给谁
	// from_user_name:lijia, 谁委托
	// op_flag:0
	@Transactional
	public ToJson<FlowRunPrcs> entrust(FlowRunPrcs flowRunPrcs,String entrstUser,String affair,String message,String context,HttpServletRequest request) {
		Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
		Users user=SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionId);
		String sqlType = (String) request.getSession().getAttribute("loginDateSouse");
		ToJson<FlowRunPrcs> toJson=new ToJson<>(1,"error");

		Users user1=null;
		Users user2=null;

		try{
			FlowRunPrcs temp=flowRunPrcsMapper.selectByPrimaryKey(flowRunPrcs.getId());
			//不能委托给自己
			if(temp!=null && temp.getUserId().equals(entrstUser)){
				toJson.setMsg("不能将工作委托给自己");
				return toJson;
			}
			//不能委托给正在办理人
			temp.setOpFlag("");
			List<String> userIds1=flowRunPrcsMapper.selNotRunById(temp);
			if(userIds1.contains(entrstUser)){
				toJson.setMsg("不能将工作委托给正在办理的人");
				return toJson;
			}
			//不能将经办工作委托给主办人
			if(!StringUtils.checkNull(flowRunPrcs.getOpFlag()) && flowRunPrcs.getOpFlag().equals("0")){
				temp.setOpFlag("1");
				temp.setPrcsId(0);
				temp.setFlowPrcs(0);
				List<String> userIds2=flowRunPrcsMapper.selNotRunById(temp);
				if(userIds2.contains(entrstUser)){
					toJson.setMsg("不能将经办工作委托给主办人");
					return toJson;
				}
			}
			//不能将主办工作委托给主办人(自己转自己情况)
			if(!StringUtils.checkNull(flowRunPrcs.getOpFlag()) && flowRunPrcs.getOpFlag().equals("1")){
				temp.setOpFlag("1");
				temp.setPrcsId(0);
				temp.setFlowPrcs(0);
				List<String> userIds2=flowRunPrcsMapper.selNotRunById(temp);
				if(userIds2.contains(entrstUser)){
					toJson.setMsg("不能将主办工作委托给主办人(自己转自己情况)");
					return toJson;
				}
			}
			int count=0;
			Map<String,Object> selParam=new HashMap<>();
			selParam.put("runId",flowRunPrcs.getRunId());
			selParam.put("prcsId",flowRunPrcs.getPrcsFlag());
			selParam.put("opFlag","0");
			selParam.put("userId",entrstUser);
			FlowRunPrcs model=flowRunPrcsMapper.selPrcsByNotRun(selParam);
			Users users=usersMapper.findUsersByuserId(entrstUser);
			if(model!=null){
				model.setOtherUser(model.getOtherUser()+","+flowRunPrcs.getUserId());
				model.setPrcsFlag("1");
				model.setOpFlag(flowRunPrcs.getOpFlag());
				count+=flowRunPrcsMapper.upPrcsById(model);
			}else{
				FlowRunPrcs insertData=new FlowRunPrcs();
				insertData.setUserId(entrstUser);
				insertData.setRunId(flowRunPrcs.getRunId());
				user2=users;
				user1=usersMapper.findUsersByuserId(flowRunPrcs.getUserId());
				insertData.setPrcsDept(users.getDeptId());
				insertData.setPrcsId(flowRunPrcs.getPrcsId());
				insertData.setPrcsFlag("1");
				insertData.setOpFlag(flowRunPrcs.getOpFlag());
				insertData.setParent(temp.getParent());
				insertData.setParentPrcsId(temp.getParentPrcsId());
				insertData.setOtherUser(flowRunPrcs.getUserId());
				insertData.setFlowPrcs(flowRunPrcs.getFlowPrcs());
				insertData.setTopFlag("0");
				insertData.setCreateTime(DateFormat.getStrDate(new Date()));
				insertData.setPrcsTime("2000-01-01 00:00:00");
				insertData.setDeliverTime("2000-01-01 00:00:00");
				insertData.setActiveTime("2000-01-01 00:00:00");
				insertData.setBackFlowPrcs(0);
				insertData.setBackPrcsId(0);
				count+=flowRunPrcsMapper.insertSelective(insertData);
			}
			FlowRunPrcs data=flowRunPrcsMapper.selectByPrimaryKey(flowRunPrcs.getId());
			data.setPrcsFlag("4");
			data.setOpFlag("0");
			data.setDeliverTime(DateFormat.getStrDate(new Date()));
			count+=flowRunPrcsMapper.upPrcsById(data);
			if(count>0){
				toJson.setFlag(0);
				toJson.setMsg("ok");
				//添加委托日志
				FlowRunLog flowRunLog=wfeLog.getFlowRunLogByRequest(request);
				flowRunLog.setType(Constant.TYPE9);
				flowRunLog.setRunId(flowRunPrcs.getRunId());
				flowRunLog.setPrcsId(flowRunPrcs.getPrcsId());
				flowRunLog.setFlowPrcs(flowRunPrcs.getFlowPrcs());
				flowRunLog.setTransactor(user2.getUserName());
				wfeLog.addFlowRunLog(flowRunLog);
			}

			//事务提醒
			if(affair!="" && affair!=null){
				/*String remindUrl = "/workflow/work/workform?opflag=1&flowId=" + flowRunPrcs.getFlowId() + "&flowStep=" + flowRunPrcs.getPrcsId()+ "&runId=" + flowRunPrcs.getRunId() + "&prcsId=" + flowRunPrcs.getFlowPrcs();
				SmsBody smsBody = new SmsBody(user.getUserId(), "7", DateFormat.getTime(DateFormat.getCurrentTime()), remindUrl, context);
				smsBody.setToId(entrstUser);
				smsService.saveSms(smsBody, entrstUser+",", "1", "1", context, context, sqlType);//存储事物提醒并推送消息
*/
				workService.todoWorkSmsBoby(entrstUser, flowRunPrcs.getFlowPrcs().toString(), "",
						flowRunPrcs.getRunId().toString(), null, String.valueOf(flowRunPrcs.getFlowId()),
						String.valueOf(flowRunPrcs.getPrcsId()-1), entrstUser, "", null, context, request, "1,", users, "", entrstUser, "");
			}
			//短信提醒
			if(message!="" && message!=null){
			/*String remindUrl = "/workflow/work/workform?opflag=1&flowId=" + flowRunPrcs.getFlowId() + "&flowStep=" + flowRunPrcs.getPrcsId()+ "&runId=" + flowRunPrcs.getRunId() + "&prcsId=" + flowRunPrcs.getFlowPrcs();
			String toId = flowRunPrcs.getUserId()+",";
			Sms2Priv sms2Priv = new Sms2Priv();
			sms2Priv.setUserId(toId);
			StringBuffer contextString  = new StringBuffer(context);
			sms2PrivServiceImpl.selSenderMobile("0",sms2Priv,contextString,request);*/
			/*	Users u =  usersMapper.findUsersByuserId(entrstUser);

				StringBuffer contextString  = new StringBuffer(context);
				sms2PrivServiceImpl.smsSenders(contextString, u.getUid()+",");*/
				workService.todoWorkSmsBoby(entrstUser, flowRunPrcs.getFlowPrcs().toString(), "",
						flowRunPrcs.getRunId().toString(), null, String.valueOf(flowRunPrcs.getFlowId()),
						String.valueOf(flowRunPrcs.getPrcsId()-1), entrstUser, "", null, context, request, "2,", users, "", entrstUser, "");


			}
		}catch (Exception e){
			e.printStackTrace();
		}

		return toJson;
	}


	/**
	 * 创建作者:   牛江丽
	 * 创建日期:   2018/1/22 13:52
	 * 方法介绍:   恢复执行
	 * 参数说明：
	 * @return
	 */
	public ToJson resumeExe(Integer runId,Integer id){
		ToJson<FlowRunPrcs> toJson=new ToJson<>(1,"error");
		try{
			FlowRun flowRun=flowRunMapper.find(runId);
			FlowRunPrcs flowRunPrcs=new FlowRunPrcs();
			int count=0;
			if(!StringUtils.checkNull(flowRun.getForceOver())){
				String[] forceOver=flowRun.getForceOver().split("`");
				flowRunPrcs.setId(id);
				flowRunPrcs.setPrcsId(Integer.valueOf(forceOver[0]));
				flowRunPrcs.setPrcsFlag(forceOver[1]);
				flowRunPrcs.setFlowPrcs(Integer.valueOf(forceOver[2]));
				flowRunPrcs.setUserId(forceOver[3]);
				flowRun.setEndTime("2000-01-01 00:00:00");
				flowRun.setForceOver("");
				count+=flowRunPrcsMapper.upPrcsStatusById(flowRunPrcs);
				count+=flowRunMapper.upRunByFouAndTime(flowRun);
			}
			if(count>0){
				toJson.setFlag(0);
				toJson.setMsg("ok");
			}
		}catch (Exception e){
			e.printStackTrace();
		}
		return toJson;
	}

	@Override
	public ToJson<FlowRunPrcs> queryFlowAssociationSet(Integer flowId) {
		ToJson<FlowRunPrcs> toJson=new ToJson<>(1,"error");
		try{
 			//先查询该流程的设置关联条件
			List<FlowRelation> flowRelations =flowAssociationSetService.queryFlowAssociationSet(flowId);
		}catch(Exception e){
			e.printStackTrace();
		}
		return toJson;
	}
	//查询会签人的是否办理完毕，会签人未办理，当前人不能办理完毕
	@Override
	public ToJson queryopaddUserOver(HttpServletRequest request,FlowRunPrcs flowRunPrcs){
		ToJson json=new ToJson();
		Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
		Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(),redisSessionCookie);
		flowRunPrcs.setOpaddUser(users.getUserId());
		List<FlowRunPrcs> flowRunPrcs1 = flowRunPrcsMapper.queryopaddUserOver(flowRunPrcs);
		if(flowRunPrcs1.size()>0){
			StringBuffer str=new StringBuffer();
			str.append("会签人:");
			for (int i=0;i<flowRunPrcs1.size();i++
				 ) {
				if(i==(flowRunPrcs1.size()-1)){
					str.append(usersMapper.getUsernameByUserId(flowRunPrcs1.get(i).getUserId()));
				}else{
					str.append(usersMapper.getUsernameByUserId(flowRunPrcs1.get(i).getUserId())+",");
				}
			}
			str.append("未办结，请等待其办理完毕！");
			json.setMsg("ok");
			json.setObject(str);
		}
			json.setFlag(0);
		return json;

	}
}